;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Stock.KqSafetyInventory", [])
        .controller("KqSafetyInventoryCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            $scope.markInfo = {
                curRow: -1
            };
            $scope.detailInfo = {};
            $scope.activeFlag = [true, false];
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/warehouseInventory/export"),
                sectionParam: {
                    ids: []
                },
                allUrl: ybHttp.RequestUrl("device", "/warehouseInventory/exportAllSafetyInventory")
            };
            $scope.condition = {};
            $scope.dataList = [];
            $scope.toolbarConfig = {
                showColConfig: false
            };
            $scope.tbConf = {
                scrollWidth: 1070,
                tableCol: [
                    {
                        name: "零件编号",
                        dataField: "materialsNo",
                        width: 170,
                        isShow: true,
                    },
                    {
                        name: "零件中文名",
                        dataField: "materialName",
                        width: 170,
                        isShow: true,
                    },
                    {
                        name: "仓库",
                        dataField: "warehouse",
                        width: 120,
                        isShow: true,
                    },
                    {
                        name: "库区",
                        dataField: "areaCode",
                        width: 120,
                        isShow: true,
                    },
                    {
                        name: "安全库存",
                        dataField: "safeNum",
                        width: 120,
                        isShow: true,
                        clickFn: "editInventory"
                    },
                    {
                        name: "实际库存",
                        dataField: "amount",
                        width: 140,
                        isShow: true,
                    },
                    {
                        name: "缺货数量",
                        dataField: "lackNum",
                        width: 140,
                        isShow: true,
                    }
                ]
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };
            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        initData();
                        break;
                    case 1:
                        break;
                    default:
                        break;
                }
            };

            function initData() {
                $scope.tabFlag = {};
                $scope.detailInfo = {};
                getDataList(1);
            }

            /* 主列表点击申请单号触发 */
            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "editInventory": // 更新库存
                        $scope.detailInfo = item;
                        $scope.changeTab(1);
                        break;
                    default:
                        break;
                }
            };

            function updateInventoryFun(service, reqUrl, param) {
                return ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return false;
                    }
                    return resp.value;
                });
            }

            $scope.updateInventory = function (isValid) {
                if (!isValid) {
                    $scope.submitted = true;
                    return;
                }
                var service = "device";
                var reqUrl = "/warehouseInventory/updateSatety";
                var param = angular.copy($scope.detailInfo);
                updateInventoryFun(service, reqUrl, param).then(function (data) {
                    if (!data) {
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0);

                });
            };

            $scope.selectSingle = function (item, list, type) {
                if (!item.isChecked) {
                    $scope.markInfo["allChecked" + type] = false;
                } else {
                    if (!checkAll(list)) {
                        $scope.markInfo["allChecked" + type] = false;
                    } else {
                        $scope.markInfo["allChecked" + type] = true;
                    }
                }
                getBillIds();
            };
            $scope.selectAll = function (list, type) {
                angular.forEach(list, function (item) {
                    item.isChecked = $scope.markInfo["allChecked" + type];
                });
                getBillIds();
            };


            function checkAll(list) {
                var selectAllFlag = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAllFlag = false;
                        break;
                    }
                    selectAllFlag = true;
                }
                return selectAllFlag;
            }

            function getBillIds() {
                $scope.exportParam.sectionParam.ids = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.ids.push(item.id);
                    }
                });
            }

            $scope.import = function () {
                var param = {
                    name: "库区级安全库存",
                    templateReqService: "device",
                    templateReqUrl: "/warehouseInventory/downloadSafetyInvenExcel",
                    importReqService: "device",
                    importReqUrl: "/warehouseInventory/importSafetyInvenExcel",
                    importErrorUrl: ""
                };
                ybWidget.ImportFile(param).then(function (data) {
                    getDataList(1);
                });
            };

            // $scope.checkDate = function (obj, startFiled, endFiled) {
            //     var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
            //     if (!!timeFormatObj.tStart) {
            //         obj[startFiled] = timeFormatObj.tStart;
            //     }
            //     if (!!timeFormatObj.tEnd) {
            //         obj[endFiled] = timeFormatObj.tEnd;
            //     }
            // };
            $scope.selectWarehouse = function () {
                ybWidget.WarehouseUsingSelector({
                    needPermit: true
                }).then(function (data) {
                    $scope.condition.warehouse = data.warehouseCode;
                })
            };
            $scope.selectArea = function () {
                if (!$scope.condition.warehouse) {
                    ybWidget.ShowToaster('error', '请选择仓库');
                    return;
                }
                var param = {};
                param.warehouseCode = $scope.condition.warehouse;
                ybWidget.DepotAreaSelector(param).then(function (data) {
                    $scope.condition.areaCode = data.areaCode;

                })
            };
            $scope.clearWarehouse = function () {
                $scope.condition.warehouse = "";
                $scope.clearArea();
            };
            $scope.clearArea = function () {
                $scope.condition.areaCode = "";
            };

            function getDataList(curPage) {
                $scope.exportParam.sectionParam.ids = [];
                $scope.markInfo.curRow = -1;
                var service = "device";
                var reqUrl = "/warehouseInventory/listSafetyInventory";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    var df = "yyyy-MM-dd HH:mm:ss";
                    angular.forEach(resp.value.list, function (item) {
                        item.deleteDateF = $filter("YbDate")(item.deleteDate, df);
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            (function () {
                getDataList();
            })();
        }])
    ;
})(window, window.angular);
